<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="重点人id" prop="personId">
        <el-input v-model="formData.personId" placeholder="请输入重点人id" />
      </el-form-item>
      <el-form-item label="票号" prop="ticketNumber">
        <el-input v-model="formData.ticketNumber" placeholder="请输入票号" />
      </el-form-item>
      <el-form-item label="0-飞机 1-铁路 2-汽车" prop="type">
        <el-radio-group v-model="formData.type">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="起点位置" prop="startLocation">
        <el-input v-model="formData.startLocation" placeholder="请输入起点位置" />
      </el-form-item>
      <el-form-item label="终点位置" prop="endLocation">
        <el-input v-model="formData.endLocation" placeholder="请输入终点位置" />
      </el-form-item>
      <el-form-item label="起点时间" prop="startTime">
        <el-date-picker
          v-model="formData.startTime"
          type="date"
          value-format="x"
          placeholder="选择起点时间"
        />
      </el-form-item>
      <el-form-item label="到达时间" prop="endTime">
        <el-date-picker
          v-model="formData.endTime"
          type="date"
          value-format="x"
          placeholder="选择到达时间"
        />
      </el-form-item>
      <el-form-item label="时长" prop="duration">
        <el-input v-model="formData.duration" placeholder="请输入时长" />
      </el-form-item>
      <el-form-item label="航班号/火车车次/汽车车次" prop="transportationNumber">
        <el-input v-model="formData.transportationNumber" placeholder="请输入航班号/火车车次/汽车车次" />
      </el-form-item>
      <el-form-item label="来源" prop="source">
        <el-input v-model="formData.source" placeholder="请输入来源" />
      </el-form-item>
      <el-form-item label="乘坐人姓名" prop="passengerName">
        <el-input v-model="formData.passengerName" placeholder="请输入乘坐人姓名" />
      </el-form-item>
      <el-form-item label="乘坐人联系电话" prop="passengerPhone">
        <el-input v-model="formData.passengerPhone" placeholder="请输入乘坐人联系电话" />
      </el-form-item>
      <el-form-item label="座位号" prop="seatNumber">
        <el-input v-model="formData.seatNumber" placeholder="请输入座位号" />
      </el-form-item>
      <el-form-item label="总价" prop="totalPrice">
        <el-input v-model="formData.totalPrice" placeholder="请输入总价" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="已读标记，0:未读，1:已读" prop="readStatus">
        <el-radio-group v-model="formData.readStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="开车时间" prop="busstarttime">
        <el-date-picker
          v-model="formData.busstarttime"
          type="date"
          value-format="x"
          placeholder="选择开车时间"
        />
      </el-form-item>
      <el-form-item label="购票时间" prop="operationtime">
        <el-date-picker
          v-model="formData.operationtime"
          type="date"
          value-format="x"
          placeholder="选择购票时间"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="carno">
        <el-input v-model="formData.carno" placeholder="请输入车牌号" />
      </el-form-item>
      <el-form-item label="检票时间" prop="checkintime">
        <el-date-picker
          v-model="formData.checkintime"
          type="date"
          value-format="x"
          placeholder="选择检票时间"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { TransportationRecordApi, TransportationRecordVO } from '@/api/member/transportationrecord'

/** 交通记录 表单 */
defineOptions({ name: 'TransportationRecordForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  personId: undefined,
  ticketNumber: undefined,
  type: undefined,
  startLocation: undefined,
  endLocation: undefined,
  startTime: undefined,
  endTime: undefined,
  duration: undefined,
  transportationNumber: undefined,
  source: undefined,
  passengerName: undefined,
  passengerPhone: undefined,
  seatNumber: undefined,
  totalPrice: undefined,
  remark: undefined,
  readStatus: undefined,
  busstarttime: undefined,
  operationtime: undefined,
  carno: undefined,
  checkintime: undefined
})
const formRules = reactive({
  personId: [{ required: true, message: '重点人id不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await TransportationRecordApi.getTransportationRecord(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as TransportationRecordVO
    if (formType.value === 'create') {
      await TransportationRecordApi.createTransportationRecord(data)
      message.success(t('common.createSuccess'))
    } else {
      await TransportationRecordApi.updateTransportationRecord(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    personId: undefined,
    ticketNumber: undefined,
    type: undefined,
    startLocation: undefined,
    endLocation: undefined,
    startTime: undefined,
    endTime: undefined,
    duration: undefined,
    transportationNumber: undefined,
    source: undefined,
    passengerName: undefined,
    passengerPhone: undefined,
    seatNumber: undefined,
    totalPrice: undefined,
    remark: undefined,
    readStatus: undefined,
    busstarttime: undefined,
    operationtime: undefined,
    carno: undefined,
    checkintime: undefined
  }
  formRef.value?.resetFields()
}
</script>